﻿Imports System.Data.OleDb
Imports System.IO
Public Class Custom_Template
    Dim grich As RichTextBox
    Dim gcon As String
    Dim gqry As String
    Dim con As OleDbConnection
    Dim ttname As String
    Dim w As WebBrowser
    Dim ds As DataSet
    Dim exn As String
    Public Sub fillreport(ByVal ob As RichTextBox, ByVal qry As String, ByVal con1 As String, ByVal tname As String, ByVal web As WebBrowser)
        grich = ob
        gqry = qry
        w = web
        gcon = con1
        Me.ShowDialog()
    End Sub
    Private Sub btndone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndone.Click
        Try
            con = New OleDbConnection(gcon)
            con.Open()
            grich.Text = ""
            Dim i As Integer
            Dim d As DataRow
            Dim ad As OleDbDataAdapter
            ad = New OleDbDataAdapter(gqry, con)
            ds = New DataSet
            ad.Fill(ds, "stock")
            Me.Show()
            grich.Text = grich.Text & "<html>" & vbCrLf
            grich.Text = grich.Text & "<center><head>" & vbCrLf
            grich.Text = grich.Text & "<TITLE> REPORT </title>" & vbCrLf
            grich.Text = grich.Text & "</head></center>" & vbCrLf
            grich.Text = grich.Text & "<body>" & vbCrLf
            If txtlogo.Text <> "" Then
                grich.Text = grich.Text & "<table border=1 width=100% & vbCrLf"
                grich.Text = grich.Text & "<tr>" & vbCrLf
                grich.Text = grich.Text & "<td width=300>" & vbCrLf
                If cmbalign.SelectedIndex = 0 Then
                    grich.Text = grich.Text & "<img src=logo" & exn & " align=" & """" & "LEFT" & """" & " width=" & txtw.Text & ComboBox1.Text & " height=" & txth.Text & ComboBox1.Text & "></img> " & vbCrLf
                Else
                    grich.Text = grich.Text & "<img src=logo" & exn & " align=" & """" & "LEFT" & """" & " width=" & txtw.Text & ComboBox1.Text & " height=" & txth.Text & ComboBox1.Text & "></img> " & vbCrLf
                End If
                grich.Text = grich.Text & "</td>" & vbCrLf
                grich.Text = grich.Text & "<td widht=100%>" & vbCrLf
                grich.Text = grich.Text & "<center>" & vbCrLf
                grich.Text = grich.Text & "<b>" & txth1.Text & "</b> <br> " & vbCrLf
                grich.Text = grich.Text & "<b>" & txth2.Text & "</b> <br> " & vbCrLf
                grich.Text = grich.Text & txth3.Text & "<br> " & vbCrLf
                grich.Text = grich.Text & "</center>" & vbCrLf
                grich.Text = grich.Text & "</td>" & vbCrLf
                grich.Text = grich.Text & "</tr>" & vbCrLf
                grich.Text = grich.Text & "</table>" & vbCrLf & "<br>"
                grich.Text = grich.Text & "<hr border=" & cmbhr1.Text & ">" & vbCrLf
            Else
                grich.Text = grich.Text & "<b>" & txth1.Text & "</b> <br> " & vbCrLf
                grich.Text = grich.Text & "<b>" & txth2.Text & "</b> <br> " & vbCrLf
                grich.Text = grich.Text & txth3.Text & "<br> " & vbCrLf
                grich.Text = grich.Text & "<hr border=" & cmbhr1.Text & ">" & vbCrLf
            End If
            grich.Text = grich.Text & "<hr border=" & """" & cmbhr2.Text & """" & "/>"
            grich.Text = grich.Text & "<table>" & vbCrLf
            grich.Text = grich.Text & "<thead>" & vbCrLf
            grich.Text = grich.Text & "<tr>" & vbCrLf
            Dim k As Integer
            For i = 0 To ds.Tables(0).Columns.Count - 1
                grich.Text = grich.Text & "<th scope=" & """" & "col" & """" & ">" & vbCrLf
                grich.Text = grich.Text & ds.Tables(0).Columns(i).ColumnName & vbCrLf
                ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                grich.Text = grich.Text & "</th>" & vbCrLf
            Next
            Dim j As Integer
            Dim f As Integer
            grich.Text = grich.Text & "</tr>" & vbCrLf
            grich.Text = grich.Text & "</thead>" & vbCrLf
            For Each d In ds.Tables(0).Rows
                grich.Text = grich.Text & "<tr>" & vbCrLf
                For i = 0 To ds.Tables(0).Columns.Count - 1
                    grich.Text = grich.Text & "<td>" & vbCrLf
                    grich.Text = grich.Text & d(i) & vbCrLf
                    grich.Text = grich.Text & "</td>" & vbCrLf
                    j = j + 1
                Next
                If j < 50000 Then
                    grich.Text = grich.Text & "</tr>" & vbCrLf
                End If
                If j >= 50000 Then
                    grich.Text = grich.Text & "</tr>" & vbCrLf
                    grich.Text = grich.Text & "</table>" & vbCrLf
                    grich.Text = grich.Text & "<table>" & vbCrLf
                    For f = 0 To ds.Tables(0).Columns.Count - 1
                        grich.Text = grich.Text & "<th>" & vbCrLf
                        grich.Text = grich.Text & ds.Tables(0).Columns(f).ColumnName & vbCrLf
                        ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                        ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                        grich.Text = grich.Text & "</th>" & vbCrLf
                    Next
                    f = 0
                    j = 0
                End If

            Next
            If j < 50000 Then
                grich.Text = grich.Text & "</table>" & vbCrLf
            End If
            grich.Text = grich.Text & "<hr border=" & """" & cmbhr2.Text & """" & "/>"
            grich.Text = grich.Text & "<font><Center>" & txtfooter.Text & "</center></font>" & vbCrLf
            grich.Text = grich.Text & "</body>" & vbCrLf
            grich.Text = grich.Text & "</html>"
            grich.SaveFile(ttname & ".html", RichTextBoxStreamType.PlainText)
            w.Navigate(Application.StartupPath & "\" & ttname & ".html")
            grich.Visible = False
            '   grich.Visible = False
            'Dim k As Integer
            'For i = 0 To ds.Tables(0).Columns.Count - 1
            '    ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
            '    ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
            'Next
            ds.Clear()
            i = 1
            con.Close()
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        txtlogo.Text = OpenFileDialog1.FileName
        If UCase(System.IO.Path.GetExtension(OpenFileDialog1.FileName)) = ".JPG" Then
            File.Copy(txtlogo.Text, "logo.jpg", True)
            exn = ".jpg"
        ElseIf UCase(System.IO.Path.GetExtension(OpenFileDialog1.FileName)) = ".PNG" Then
            File.Copy(txtlogo.Text, "logo.png", True)
            exn = ".png"
        ElseIf UCase(System.IO.Path.GetExtension(OpenFileDialog1.FileName)) = ".GIF" Then
            File.Copy(txtlogo.Text, "logo.gif", True)
            exn = ".gif"
        End If
    End Sub

    Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub txtw_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtw.KeyPress, txth.KeyPress
        If IsNumeric(e.KeyChar) = False And Asc(e.KeyChar) <> 8 Then
            e.KeyChar = ""
        End If
    End Sub
End Class